#!/usr/bin/python

from __future__ import print_function
from world import World
from character import Character
import sys

if __name__ == '__main__':
    myworld = World(20)
    pos = (0, 0)
    for pos,name,env in (((4,8),'Luoyang','%tR0'), \
                        ((10,11),'Yangzhou','-tC0'), \
                        ((8,2),'Chengdu','^tR0'), \
                        ((4,4),'Changan','#tR0')):
        myworld.namePlace(pos, name)
        myworld.setPlaceEnv(pos, env)

    characters = {}
    bob = Character('Bob Smith', 21, 0, 5, 5)
    myworld.addToPlace(bob.position, bob)
    characters.update({'bob' : bob})
    sue = Character('Sue Smith', 18, 2500, 4, 4)
    myworld.addToPlace(sue.position, sue)
    characters.update({'sue' : sue})
    tom = Character('Tom Jones', 28, 11, 9, 8)
    myworld.addToPlace(tom.position, tom)
    characters.update({'tom' : tom})

    ego = sue
    command = ['show']
    direction = { 'north':(-1,0), 'n':(-1,0), 'south':(1,0),'s':(1,0),
                  'east': (0,1), 'e':(0,1), 'west' : (0,-1), 'w':(0,-1),
                  'northeast' : (-1,1), 'ne':(-1,1),
                  'southeast':(1,1),'se':(1,1),
                  'northwest' : (-1,-1), 'southwest' : (1,-1),
                  'nw': (-1,-1), 'sw': (1,-1)}
    while (command[0]):
        command = raw_input('Enter command: ').split()
        if command[0] == 'show':
            if len(command) < 2:
                myworld.printMap()
            elif command[1] in list(characters.keys()):
                characters[command[1]].showCharacter()
            else:
                print("Sorry, I don't know who %s is" % command[1])
                continue
        elif command[0] == 'go':
            if len(command) < 2:
                print('Sorry, I need a direction')
                continue
            elif command[1] not in list(direction.keys()):
                print('Sorry, I need a direction')
                continue
            newlat = ego.position[0] + direction[command[1]][0]
            newlong = ego.position[1] + direction[command[1]][1]
            myworld.moveFromToPlace(ego.position,(newlat,newlong),ego)
            myworld.printMap()
        elif command[0] == 'become':
            if len(command) < 2:
                print('Sorry, I need the name of the person to become')
                continue
            elif command[1] not in list(characters.keys()):
                print("Sorry, I don't know that person")
                continue
            ego = characters[command[1]]
        elif command[0] == 'quit':
            break
        else:
            print("Sorry, I don't understand that")
